package com.info3k.client.commands.user;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.info3k.client.Info3K;
import com.info3k.client.components.GlassDialogBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.validators.PasswordTextBoxValidator;
import com.info3k.client.domain.User;
import com.info3k.client.enums.TabTypes;

public class ChangePasswordCommand extends GlassDialogBox implements Command {
		
	private Info3K info3k;
	
	private Button changePasswordButton = new Button(Info3K.msgs.changePassword());
	
	private PasswordTextBoxValidator oldPassword = new PasswordTextBoxValidator();
	private PasswordTextBoxValidator newPassword = new PasswordTextBoxValidator();
	private PasswordTextBoxValidator newPasswordAgain = new PasswordTextBoxValidator();
	
	private User user;
	
	public ChangePasswordCommand(Info3K info, final Command cmd, User u){
		this.info3k = info;
		this.user = u;
		final Label lblOldPassword = new Label(Info3K.msgs.oldPassword() + ": ");
		final Label lblNewPassword = new Label(Info3K.msgs.newPassword() + ": ");
		final Label lblNewPasswordAgain = new Label(Info3K.msgs.newPasswordAgain() + ": ");
		setText(Info3K.msgs.changePassword());
		content.add(lblOldPassword);
		content.add(oldPassword);
		content.add(lblNewPassword);
		content.add(newPassword);
		content.add(lblNewPasswordAgain);
		content.add(newPasswordAgain);
		
		oldPassword.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				oldPassword.setValid(oldPassword.getText().length()>5);
			}
		});
		
		newPassword.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				newPassword.setValid(newPassword.getText().length()>5);
			}
		});
		
		newPasswordAgain.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				newPasswordAgain.setValid(newPasswordAgain.getText().length()>5 && newPasswordAgain.getText().equals(newPassword.getText()));
				if(!newPasswordAgain.getText().equals(newPassword.getText())){
					InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.passwordsNotEquals());
				}
			}
		});
		
		footer.add(changePasswordButton);
		
		changePasswordButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(!newPassword.getText().equals(newPasswordAgain.getText())){
					InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.passwordsNotEquals());
					return;
				}
				if(oldPassword.isValid() && newPassword.isValid() && newPasswordAgain.isValid()){
					Info3K.loader.setVisible(true);
				
					Info3K.infoService.changePasswordForUser(user, oldPassword.getText(), newPasswordAgain.getText(), new AsyncCallback<User>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(User result) {
							Info3K.loader.setVisible(false);
							if(result!=null){
								if(cmd instanceof EditUserCommand){
									((EditUserCommand)cmd).setUser(result);
								}
								hide();
								InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.passwordChangedSuccessfully());
								info3k.getLoginManager().setUser(result);
								info3k.getTabManager().refresh(TabTypes.LATEST_USERS_LEFT);
								info3k.getTabManager().refresh(TabTypes.LATEST_USERS_TOP);
							} else {
								InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.incorrectOldPassword());
							}
						}
						
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		changePasswordButton.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(closeButton);
	}
	
	public void execute() {
		center();
	}
}
